package com.example.memo1.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.memo1.bean.Record;
import com.example.memo1.dao.impl.AdminDaoImpl;
import com.example.memo1.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class AdminDao implements AdminDaoImpl {

    SQLiteDatabase db= DBUtil.db;
    @Override
    public int registerAdmin(String ...da){
        try{
        db.execSQL("INSERT INTO d_admin VALUES(?,?,?,?,?,?)",da);
        return 1;
        }catch (Exception e){
            return -1;
        }
    }
    @Override
    public int loginAdmin(String ...da){

        int a=0;
        Cursor res=db.rawQuery("select*from d_admin where s_id=? and s_pwd=?",da);
        if(res.moveToNext()){
            a++;
        }
        return a;
    }

    /**
     * 删除信息
     * @param da
     * @return
    **/
      @Override
        public int delRecord(String... da) {
              try{
                 db.execSQL("delete from d_record where s_id=? and s_uid=?",da);
                return 1;
             }catch (Exception e){
                 return -1;
             }

          }


    @Override
    public int saveRecord(String... da) {//id 标题 内容 用户ID 时间
        try{
            db.execSQL("INSERT INTO d_record VALUES(?, ?, ?, ?, ?)", da);
            return 1;
        }catch (Exception e){

            String daa[]={da[1],da[2],da[4],da[0],da[3]};
            try{
                 db.execSQL("update d_record set s_title=? ,s_con=?,s_time=? where s_id=? and s_uid=?",da);
                 return 1;
            }catch (Exception ee){
                e.printStackTrace();
                ee.printStackTrace();
                return -1;
            }


        }

    }

    @Override
    public int changePassword(String... da) {
        ContentValues values=new ContentValues();
        values.put("s_pwd",da[1]);
        String das[]={da[0]};
          try{
              return  db.update("d_admin",values," s_id=?",das);

          }catch (Exception e){
return  -1;
          }

    }


    public List<Record> showRecord(){

        List<Record> re=new ArrayList<>();//存储所有备忘录
        Cursor res = db.rawQuery("select * from d_record",null);
        while(res.moveToNext()){
            Record record=new Record(res.getString(0),res.getString(1),res.getString(2),res.getString(3),res.getString(4));
            re.add(record);
        }
        return re;
    }

    public List<Record> searchRecord(String s_id, String s_title, String s_con) {
        List<Record> re = new ArrayList<>();
        Cursor res = db.rawQuery("SELECT * FROM d_record WHERE s_title LIKE ? OR s_con LIKE ?",
                new String[]{ s_title + "%", "%" + s_con + "%"});
        while (res.moveToNext()) {
            Record record = new Record(res.getString(0), res.getString(1), res.getString(2), res.getString(3), res.getString(4));
            re.add(record);
        }
        return re;
    }


}
